import React from  'react'
import router from 'umi/router'
import { connect } from 'dva'
import MessageModel from 'components/common/MessageModel'
import { repairCancel, repairStopByforce, repairDel } from 'dic/messageModel'
import tools from '../../../common/tools'
import { message } from 'antd'
import './index.styl'

function ListActions({ dispatch, singleData, type, list }) {
    // 操作完成之后需要初始化数据-----------------------------------------------------
    // 处理操作的结果
    const getInintForm = (res) => {
        if (res.code === '000000') {
            message.success(res.mesg)
        } else {
            message.error(res.mesg)
        }
        // 清除缓存
        tools.cleanCache('repairSingle')
        // 初始化数据
        dispatch({
            type: 'deviceRepair/getSingleList',
            payload: {
                current: '1',
                size: '10',
            }
        })
    }

    // 确定删除维修单
    const deleteSingle = (preStatus) => {
        dispatch({
            type: 'deviceRepair/getRepairUpdate',
            payload: {
                preStatus,
                orderId: list.orderId,
            },
        }).then(res => {
            getInintForm(res)
        })
    }

    // 确定取消维修单
    const isCancel = (preStatus) => {
        dispatch({
            type: 'deviceRepair/getRepairUpdate',   
            payload: {
                preStatus,
                orderId: list.orderId,
                ifPass: false,
            },
        }).then(res => {
            getInintForm(res)
        })
    }

    // 确定强制取消维修单
    const constraintCancel = (preStatus) => {
        dispatch({
            type: 'deviceRepair/getRepairUpdate',
            payload: {
                preStatus,
                orderId: list.orderId,
                ifPass: true,
            },
        }).then(res => {
            getInintForm(res)
        })
    }

    // 同意取消，拒绝取消
    const isAgreeCancel = (preStatus, agree) => {
        dispatch({
            type: 'deviceRepair/getRepairUpdate',
            payload: {
                preStatus,
                orderId: list.orderId,
                ifPass: agree,
            },
        }).then(res => {
            getInintForm(res)
        })
    }

    // //  操作完之后不需要初始化数据---------------------------------------------------
    // // 查看维修单详情
    const toDetail = (id) => {
        router.push({
            pathname: '/deviceRepair/RepairDetails',
            query: {
                listState: list.orderId,
                actionId: list.status,
                procInsId: list.procInsId,
                orderId: list.orderId,
                // singleData: JSON.stringify(singleData),
                status: id,
            },
        })
    }

    // 确认申请前的校验
    const toEnterDetail = (id) => {
        dispatch({
            type: 'deviceRepair/getChickDetail',
            payload: { orderId: list.orderId },
          }).then(res => {
              // 改条数据已被其他账号做除删除或其他操作
                if (res.code === '000000') {
                  if (res.data.status === 5) {
                    router.push({
                        pathname: '/deviceRepair/RepairDetails',
                        query: {
                            listState: list.orderId,
                            actionId: list.status,
                            procInsId: list.procInsId,
                            orderId: list.orderId,
                            // singleData: JSON.stringify(singleData),
                            status: id,
                        },
                    })
                    return
                } else {
                message.error('订单状态已变更')
                }
                    // 清除缓存
                    tools.cleanCache('repairSingle')
                    // 初始化数据
                    dispatch({
                        type: 'deviceRepair/getSingleList',
                        payload: {
                            current: '1',
                            size: '10',
                        }
                    })
            } else {
                getInintForm(res)
            }
               
        })
    }

    const user = JSON.parse(localStorage.getItem('userMsg')) && JSON.parse(localStorage.getItem('userMsg')).username

    const shouwButton = (key, listdata) => {
        switch(Number(key)){
            case 2:  // "待企业审批"
                return list.approvaluername === user ? <span className='btnStyl' onClick={() => toDetail(2)}>审批</span> : null;
            case 3: // "待平台审核"
                return null ;
            case 4: // "待接单"
                return !listdata.orderLockTime ? <MessageModel data={repairDel} handleClick={() => deleteSingle(4)}/> : null;
            case 5: // "待确认"
                return <span className='btnStyl' onClick={() => toEnterDetail(5)}>确认申请</span> ;
            case 6: // 待领件 
                return <>
                {
                    listdata.operation === 1 || !listdata.operation
                    ?  
                    <MessageModel data={repairCancel} handleClick={() => isCancel(6)}/>
                    :
                    null
                }
                {
                    Number(listdata.operation) === 1002 && Number(listdata.userChannel) === 2
                    ?  
                    <MessageModel data={repairStopByforce} handleClick={() => constraintCancel(6)}/>
                    :
                    null
                }
                </>;
            case 7: // 待维修
                return <>
                {
                    (listdata.operation === 1001 || !listdata.operation)
                    ?
                    (Number(list.usedSpares) === 0 ? <MessageModel data={repairCancel} handleClick={() => isCancel(7)}/> : null)
                    :
                    null
                }
                {
                    Number(list.usedSpares) === 0 
                    && Number(listdata.operation) === 1002 
                    && Number(listdata.userChannel) === 2
                    ?
                    <MessageModel data={repairStopByforce} handleClick={() => constraintCancel(7)}/>
                    :
                    null
                }
                </> ;
            case 8: // "维修中";
                return null ;
            case 9: // "待验收";
                return <span className='btnStyl' onClick={() => toDetail(9)}>验收</span> ;
            case 10: // "已完成";
                return  null ;
            case 11: // "取消中"
                return <>
                {
                    Number(list.waitRepairUserChannel) === 2 
                    ?
                    <>
                        <span className='btnStyl' onClick={() => isAgreeCancel(11, true)}>同意取消</span>
                        <span className='btnStyl' onClick={() => isAgreeCancel(11, false)}>拒绝取消</span>
                    </>
                    :
                    null
                }
               </> ;
            case 12:
                return null //"超时未完成";
            case 13:
                return null // "企业审批失败";
            case 14:
                return null // "已删除";
            default:
                return;
        }
    }

    return(
        <div className='list_actions'>
            {shouwButton(list.status, list)}
            <span className='btnStyl' onClick={() => toDetail(type)}>查看详情</span>
        </div>
    )
}

export default connect(({ deviceRepair }) => ({
    deviceRepair
  }))(ListActions)